package com.fanyue.modules.wzb.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.base.BaseServiceImpl;
import com.fanyue.core.cache.CoolCache;
import com.fanyue.core.exception.CoolPreconditions;
import com.fanyue.core.request.R;
import com.fanyue.core.util.CoolSecurityUtil;
import com.fanyue.core.util.DatabaseDialectUtils;
import com.fanyue.modules.base.entity.sys.BaseSysParamEntity;
import com.fanyue.modules.base.entity.sys.BaseSysUserEntity;
import com.fanyue.modules.base.service.sys.BaseSysParamService;
import com.fanyue.modules.base.service.sys.BaseSysUserService;
import com.fanyue.modules.wzb.entity.*;
import com.fanyue.modules.wzb.mapper.ClothingBatchItemMapper;
import com.fanyue.modules.wzb.service.*;
import com.fanyue.modules.wzb.utils.CodeUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.tangzc.autotable.core.constants.DatabaseDialect;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.fanyue.modules.base.entity.sys.table.BaseSysDepartmentEntityTableDef.BASE_SYS_DEPARTMENT_ENTITY;
import static com.fanyue.modules.base.entity.sys.table.BaseSysUserEntityTableDef.BASE_SYS_USER_ENTITY;
import static com.fanyue.modules.wzb.entity.table.AppUserEntityTableDef.APP_USER_ENTITY;
import static com.fanyue.modules.wzb.entity.table.CargoEntityTableDef.CARGO_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingBatchItemEntityTableDef.CLOTHING_BATCH_ITEM_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingInfoEntityTableDef.CLOTHING_INFO_ENTITY;


/**
 * 系统用户
 */
@Service
@RequiredArgsConstructor
public class ClothingBatchItemServiceImpl extends BaseServiceImpl<ClothingBatchItemMapper, ClothingBatchItemEntity> implements ClothingBatchItemService {

    @Override
    public Object page(JSONObject requestParams, Page<ClothingBatchItemEntity> page, QueryWrapper qw) {
        Long batchId = requestParams.getLong("batchId");
        String code = requestParams.getStr("keyWord");
        Integer category = requestParams.getInt("category");
        Integer season = requestParams.getInt("season");
        Integer status = requestParams.getInt("status");
        Long specId = requestParams.getLong("specId");
        String databaseDialect = DatabaseDialectUtils.getDatabaseDialect();
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
        } else {
            qw.select(CLOTHING_BATCH_ITEM_ENTITY.ALL_COLUMNS,
                    CLOTHING_INFO_ENTITY.CODE.as("code"),
                    CLOTHING_INFO_ENTITY.CATEGORY.as("category"),
                    CLOTHING_INFO_ENTITY.SEASON.as("season"),
                    CLOTHING_INFO_ENTITY.SPEC_ID.as("specId"));
        }
        qw.from(CLOTHING_BATCH_ITEM_ENTITY)
                .leftJoin(CLOTHING_INFO_ENTITY)
                .on(CLOTHING_BATCH_ITEM_ENTITY.CLOTHING_ID.eq(CLOTHING_INFO_ENTITY.ID));

        if (batchId != null) {
            qw.and(CLOTHING_BATCH_ITEM_ENTITY.BATCH_ID.eq(batchId));
        }
        if (StrUtil.isNotEmpty(code)) {
            qw.and(CLOTHING_INFO_ENTITY.CODE.like(code));
        }
        if (category != null) {
            qw.and(CLOTHING_INFO_ENTITY.CATEGORY.eq(category));
        }
        if (season != null) {
            qw.and(CLOTHING_INFO_ENTITY.SEASON.eq(season));
        }
        if (status != null) {
            qw.and(CLOTHING_INFO_ENTITY.STATUS.eq(status));
        }
        if (specId != null) {
            qw.and(CLOTHING_INFO_ENTITY.SPEC_ID.eq(specId));
        }
        //qw.isNotNull("wzb_clothing_info.id");
        return mapper.paginate(page, qw);
    }

    @Override
    public List<ClothingBatchItemEntity> listByInfo(ClothingBatchItemEntity info) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(CLOTHING_BATCH_ITEM_ENTITY.ALL_COLUMNS);
        queryWrapper.from(CLOTHING_BATCH_ITEM_ENTITY);
        queryWrapper.and(CLOTHING_BATCH_ITEM_ENTITY.STATUS.eq(info.getStatus(), info.getStatus() != null));
        queryWrapper.and(CLOTHING_BATCH_ITEM_ENTITY.BATCH_ID.eq(info.getBatchId(), info.getBatchId() != null));
        queryWrapper.and(CLOTHING_BATCH_ITEM_ENTITY.CLOTHING_ID.eq(info.getClothingId(), info.getClothingId() != null));

        return mapper.selectListByQuery(queryWrapper);
    }
}